package com.netease.yunxin.app.im.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.zhht.htzx.R;
import com.netease.yunxin.app.im.model.ProductMessage;

/**
 * 商品消息显示组件
 */
public class ProductMessageView extends LinearLayout {
    
    private ImageView ivProductImage;
    private TextView tvProductName;
    private TextView tvProductPrice;
    private TextView tvProductDesc;
    private TextView tvSellerName;
    private TextView tvViewDetail;
    
    private ProductMessage productMessage;
    
    public ProductMessageView(Context context) {
        super(context);
        init(context);
    }
    
    public ProductMessageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    
    public ProductMessageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    
    private void init(Context context) {
        setOrientation(VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.view_product_message, this, true);
        
        ivProductImage = findViewById(R.id.iv_product_image);
        tvProductName = findViewById(R.id.tv_product_name);
        tvProductPrice = findViewById(R.id.tv_product_price);
        tvProductDesc = findViewById(R.id.tv_product_desc);
        tvSellerName = findViewById(R.id.tv_seller_name);
        tvViewDetail = findViewById(R.id.tv_view_detail);
        
        // 设置点击事件
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openProductDetail();
            }
        });
        
        tvViewDetail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openProductDetail();
            }
        });
    }
    
    /**
     * 设置商品消息数据
     * @param productMessage 商品消息对象
     */
    public void setProductMessage(ProductMessage productMessage) {
        this.productMessage = productMessage;
        if (productMessage == null) {
            return;
        }
        
        // 设置商品名称
        tvProductName.setText(productMessage.getProductName());
        
        // 设置商品价格
        tvProductPrice.setText(productMessage.getProductPrice());
        
        // 设置商品描述
        tvProductDesc.setText(productMessage.getProductDesc());
        
        // 设置卖家名称
        tvSellerName.setText("卖家: " + productMessage.getSellerName());
        
        // 加载商品图片
        if (productMessage.getProductImage() != null && !productMessage.getProductImage().isEmpty()) {
            Glide.with(getContext())
                    .load(productMessage.getProductImage())
                    .placeholder(R.drawable.app_white_round_bg)
                    .error(R.drawable.app_white_round_bg)
                    .into(ivProductImage);
        }
    }
    
    /**
     * 打开商品详情页
     */
    private void openProductDetail() {
        if (productMessage != null && productMessage.getProductUrl() != null 
                && !productMessage.getProductUrl().isEmpty()) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(productMessage.getProductUrl()));
                getContext().startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 获取商品消息对象
     * @return 商品消息对象
     */
    public ProductMessage getProductMessage() {
        return productMessage;
    }
} 